using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ALib;
using ALib.Data.IO;
using AxTWSLib;
using Remoting;
using TradingPlatform.IB.Operations;
using Timer=System.Windows.Forms.Timer;
using TradingPlatform;

namespace TradingPlatform.IB
{
    public partial class InteractiverBrokers : Form
    {
        //private AxTws _tws;
        private Comm _comm;

        private HistoricalBar _historicalBars;

        public HistoricalBar HistoricalBars
        {
            get { return _historicalBars; }
        }
        private MarketData _marketData;

        public MarketData MarketData
        {
            get { return _marketData; }
        }
        private ContractDetails _contractDetails;

        public ContractDetails ContractDetails
        {
            get { return _contractDetails; }
        }
        private ManagedAccounts _managedAccounts;

        public ManagedAccounts ManagedAccounts
        {
            get { return _managedAccounts; }
        }
        private AccountUpdates _accountUpdates;

        public AccountUpdates AccountUpdates
        {
            get { return _accountUpdates; }
        }
        private OrderId _orderId;

        public OrderId OrderId
        {
            get { return _orderId; }
        }
        private PlaceOrder _placeOrder;

        public PlaceOrder PlaceOrder
        {
            get { return _placeOrder; }
        }
        //private OpenOrders _openOrders;

        //public OpenOrders OpenOrders
        //{
        //    get { return _openOrders; }
        //}
        //private AllOpenOrders _allOpenOrders;

        //public AllOpenOrders AllOpenOrders
        //{
        //    get { return _allOpenOrders; }
        //}

        private readonly OrderManagementDb _orderManagementDb;

        public OrderManagementDb OrderManagementDb
        {
            get { return _orderManagementDb; }
        }

        private readonly List<string> _errorMessageList;

        public List<string> ErrorMessageList
        {
            get { return _errorMessageList; }
        } 

        public InteractiverBrokers(Comm comm)
        {
            InitializeComponent();
            _orderManagementDb = new OrderManagementDb();
            _errorMessageList = new List<string>();
            _comm = comm;
        }

        public string Connect()
        {
            Configuration configuration = new Configuration(@"Configuration\ConnectionToIb.txt");
            string ip = configuration.GetString(KeyWordForIbConnection.InteractiveBrokerIp);
            int port = configuration.GetInt(KeyWordForIbConnection.InteractiveBrokerPort);
            int clientId = configuration.GetInt(KeyWordForIbConnection.InteractiveBrokerClientId);

            //_tws = new AxTws();
            //_tws.BeginInit();
            //_tws.Enabled = true;
            //_tws.Location = new System.Drawing.Point(0, 0);
            //_tws.Visible = false;
            //_tws.Name = "_tws";
            //Controls.Add(_tws);
            //_tws.EndInit();
            //_tws.connect(ip, port, clientId);
            //_comm = new Comm(ip, port, this);
            _comm.DoConnect(ip, port);

            //if (_tws.serverVersion>0)
            {
                InitTradingComponents();
            }
            return "";
        }

        public void Disconnect()
        {
            //_tws.disconnect();
           // _tws.Dispose();
        }

        private void InitTradingComponents()
        {
            _marketData = new MarketData(_comm);
            //_historicalBars = new HistoricalBar(_tws);
            //_contractDetails = new ContractDetails(_tws);
            //_managedAccounts = new ManagedAccounts(_tws);
            //_accountUpdates = new AccountUpdates(_tws);
            _orderId = new OrderId();
            _placeOrder = new PlaceOrder(_comm, _orderManagementDb);
            //_openOrders = new OpenOrders(_tws);
            //_allOpenOrders = new AllOpenOrders(_tws);

            //_tws.errMsg += new _DTwsEvents_errMsgEventHandler(_tws_errMsg);
        }

        void _tws_errMsg(object sender, _DTwsEvents_errMsgEvent e)
        {
            string errorMessage = string.Format(@"{0},{1},{2}", e.id, e.errorCode, e.errorMsg);
            _errorMessageList.Add(errorMessage);
        }
    }
}